package com.ruoyi.binance.controller;

import com.ruoyi.binance.domain.BinanceMinKConf;
import com.ruoyi.binance.service.AsyncKData;
import com.ruoyi.binance.service.IBinanceMinKConfService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * K线关注Controller
 *
 * @author zq
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/binance/kConf")
public class BinanceMinKConfController extends BaseController {
    @Autowired
    private IBinanceMinKConfService binanceMinKConfService;
    @Autowired
    private AsyncKData asyncMink;

    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<BinanceMinKConf> list = binanceMinKConfService.selectBinanceMinKConfList(new BinanceMinKConf());
        return success(list);
    }

    /**
     * 查询K线关注列表
     */
    @PreAuthorize("@ss.hasPermi('binance:kConf:list')")
    @GetMapping("/list")
    public TableDataInfo list(BinanceMinKConf binanceMinKConf) {
        startPage();
        List<BinanceMinKConf> list = binanceMinKConfService.selectBinanceMinKConfList(binanceMinKConf);
        return getDataTable(list);
    }

    /**
     * 导出K线关注列表
     */
    @PreAuthorize("@ss.hasPermi('binance:kConf:export')")
    @Log(title = "K线关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BinanceMinKConf binanceMinKConf) {
        List<BinanceMinKConf> list = binanceMinKConfService.selectBinanceMinKConfList(binanceMinKConf);
        ExcelUtil<BinanceMinKConf> util = new ExcelUtil<BinanceMinKConf>(BinanceMinKConf.class);
        util.exportExcel(response, list, "K线关注数据");
    }

    /**
     * 获取K线关注详细信息
     */
    @PreAuthorize("@ss.hasPermi('binance:kConf:query')")
    @GetMapping(value = "/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code) {
        return success(binanceMinKConfService.selectBinanceMinKConfByCode(code));
    }

    /**
     * 新增K线关注
     */
    @PreAuthorize("@ss.hasPermi('binance:kConf:add')")
    @Log(title = "K线关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BinanceMinKConf binanceMinKConf) {
        int i = binanceMinKConfService.insertBinanceMinKConf(binanceMinKConf);
        if (i > 0) {
            asyncMink.saveKData(binanceMinKConf.getCode());
        }
        return toAjax(i);
    }

    /**
     * 修改K线关注
     */
    @PreAuthorize("@ss.hasPermi('binance:kConf:edit')")
    @Log(title = "K线关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BinanceMinKConf binanceMinKConf) {
        return toAjax(binanceMinKConfService.updateBinanceMinKConf(binanceMinKConf));
    }

    /**
     * 删除K线关注
     */
    @PreAuthorize("@ss.hasPermi('binance:kConf:remove')")
    @Log(title = "K线关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codes}")
    public AjaxResult remove(@PathVariable String[] codes) {
        return toAjax(binanceMinKConfService.deleteBinanceMinKConfByCodes(codes));
    }
}
